package lntu.cn.cxxd0927;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    String[] nameList={"刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽","刘备","张飞","关羽"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView=findViewById(R.id.lv_name);
        //适配器
        MyAdapter myAdapter=new MyAdapter();
        //为列表添加适配器
        listView.setAdapter(myAdapter);

    }

    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return nameList.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                holder=new ViewHolder();
                convertView=View.inflate(getApplicationContext(),R.layout.item,null);
                holder.tvName=convertView.findViewById(R.id.tv);
                convertView.setTag(holder);
            }else{
                holder=(ViewHolder)convertView.getTag();
            }
            holder.tvName.setText(nameList[position]);
            return convertView;
        }
    }
}
